class Account < ActiveRecord::Base

  has_many :entries,
           :order => 'entries.transaction_date, entries.id',
           :include => :payee

  def balance
    Money.new(entries.sum(:credit).to_i + (-entries.sum(:debit).to_i))
  end

  def daily_totals
    connection.select_all(<<-SQL.gsub(/[\s\n]+/, ' '))
      SELECT e.transaction_date,

      (select sum(-ee.debit + ee.credit)
        from entries ee
        where ee.transaction_date <= e.transaction_date
        order by ee.transaction_date
        ) as daily_total FROM entries e where

      e.account_id = 1

      group by e.transaction_date
      order by e.transaction_date
    SQL
  end

  def parse_ofx
    # take ofx, loop account transactions do:
    # entry.new
  end
end
